package com.rc.saas.tenant.service.member;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.common.constant.WebConstant;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.mapper.member.MemberAccountMapper;
import com.rc.saas.tenant.mapper.member.MemberAccountRecordMapper;
import com.rc.saas.tenant.model.member.MemberAccount;
import com.rc.saas.tenant.model.member.MemberAccountRecord;
import com.rc.saas.tenant.search.member.MemberAccountSearch;
import com.rc.saas.tenant.vo.member.MemberAccountResult;
import com.rc.saas.tenant.vo.member.MemberRechargeVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class MemberAccountServiceImpl implements MemberAccountService {
    private static Logger logger = LogManager.getLogger(MemberAccountServiceImpl.class);

    @Autowired
    private MemberAccountMapper memberAccountMapper;

    @Autowired
    private MemberAccountRecordMapper memberAccountRecordMapper;

    @Override
    public MemberAccountMapper getMapper() {
        return memberAccountMapper;
    }

    //@Override
    //public Long insertReturnId(MemberAccount memberAccount) {
    //    return memberAccountMapper.insertReturnId(memberAccount);
    //}

    @Override
    public void saveMemberRecharge(MemberRechargeVo memberRecharge) {
        MemberAccount memberAccount = memberAccountMapper.selectByPrimaryKey(memberRecharge.getMemberId());
        if (memberAccount == null) {
            logger.error("找不到会员余额记录:{}", memberRecharge.getMemberId());
            throw new RuntimeException("找不到会员余额记录");
        }

        //添加充值记录
        MemberAccountRecord accountRecord = new MemberAccountRecord();
        accountRecord.setMemberId(memberRecharge.getMemberId());

        //记录类型(0会员在线充值,1支出,2转账收入,3转账支出,4后台订单调整,5平台赠送,6积分兑换 7回收金额 8平台钱包充值 9分销返利 10资源占用费 11平台扣费 )
        if (memberRecharge.getAmount().compareTo(BigDecimal.ZERO) < 0) {
            accountRecord.setRecordType("11");

            if (StringUtils.isNotBlank(memberRecharge.getRemark())) {
                accountRecord.setRemark(memberRecharge.getRemark());
            } else {
                accountRecord.setRemark("平台扣费");
            }
        } else {
            accountRecord.setRecordType("8");

            if (StringUtils.isNotBlank(memberRecharge.getRemark())) {
                accountRecord.setRemark(memberRecharge.getRemark());
            } else {
                accountRecord.setRemark("平台钱包充值");
            }
        }

        accountRecord.setAmount(memberRecharge.getAmount());

        if ("1".equals(memberRecharge.getMemberType())) {
            memberAccount.setEnterpriseAccount(memberAccount.getEnterpriseAccount().add(memberRecharge.getAmount()));
            memberAccountMapper.updateByPrimaryKeySelective(memberAccount);

            accountRecord.setAccountType("1");


            //累计后的account
            accountRecord.setResult(memberAccount.getEnterpriseAccount());
        } else {
            MemberAccount updateMemberAccount=new MemberAccount();
            updateMemberAccount.setMemberId(memberAccount.getMemberId());
            updateMemberAccount.setAccount(memberAccount.getAccount().add(memberRecharge.getAmount()));
            memberAccountMapper.updateByPrimaryKeySelective(updateMemberAccount);
           // memberAccountMapper.updateMemberAccountByMemberId(memberRecharge);

            //支付的账户类型 0个人钱包 1企业钱包 默认0
            accountRecord.setAccountType(WebConstant.STATUS_ZERO);


            //累计后的account
            accountRecord.setResult(updateMemberAccount.getAccount());

        }
        memberAccountRecordMapper.insertSelective(accountRecord);
    }

    @Override
    public void updateMemberAccountByMigrateId(MemberAccount memberAccount) {
        memberAccountMapper.updateMemberAccountByMigrateId(memberAccount);
    }

    @Override
    public TResponse saveEnterpriseAmount(Long enterpriseMemberId, Long enterpriseSubmemberId, BigDecimal enterpriseAccount) {
        MemberAccount enterpriseAmount = memberAccountMapper.selectByPrimaryKey(enterpriseMemberId);
        if (enterpriseAmount.getEnterpriseAccount().compareTo(enterpriseAccount) < 0) {
            return TResponse.FAIL("企业余额不足");
        }
        MemberAccount submemberAccount = memberAccountMapper.selectByPrimaryKey(enterpriseSubmemberId);

        MemberAccountRecord memberAccountRecord = new MemberAccountRecord();
        memberAccountRecord.setTenantCode(enterpriseAmount.getTenantCode());
        memberAccountRecord.setMemberId(enterpriseAmount.getMemberId());
        //支付的账户类型 0个人钱包 1企业钱包 默认0
        memberAccountRecord.setAccountType("1");
        //记录类型(0会员在线充值,1支出,2转账收入,3转账支出,4后台订单调整,5平台赠送,6积分兑换 7回收金额 8平台钱包充值 9分销返利 10资源占用费 11平台扣费 12 数据迁移)
        memberAccountRecord.setRecordType("3");
        memberAccountRecord.setRemark("转账支出");
        memberAccountRecord.setAmount(enterpriseAccount);
        memberAccountRecord.setResult(enterpriseAmount.getEnterpriseAccount().subtract(enterpriseAccount));
        memberAccountRecordMapper.insertSelective(memberAccountRecord);

        memberAccountRecord.setMemberId(submemberAccount.getMemberId());
        memberAccountRecord.setAmount(enterpriseAccount);
        memberAccountRecord.setResult(submemberAccount.getEnterpriseAccount().add(enterpriseAccount));
        memberAccountRecord.setRemark("转账收入");
        memberAccountRecordMapper.insertSelective(memberAccountRecord);

        MemberAccount enterpriseAmountUpdate = new MemberAccount();
        enterpriseAmountUpdate.setMemberId(enterpriseAmount.getMemberId());
        enterpriseAmountUpdate.setEnterpriseAccount(enterpriseAmount.getEnterpriseAccount().subtract(enterpriseAccount));
        memberAccountMapper.updateByPrimaryKeySelective(enterpriseAmountUpdate);

        MemberAccount submemberAmountUpdate = new MemberAccount();
        submemberAmountUpdate.setMemberId(submemberAccount.getMemberId());
        submemberAmountUpdate.setEnterpriseAccount(submemberAccount.getEnterpriseAccount().add(enterpriseAccount));
        submemberAmountUpdate.setAllTransferAmount(submemberAccount.getAllTransferAmount().add(enterpriseAccount));
        memberAccountMapper.updateByPrimaryKeySelective(submemberAmountUpdate);

        return TResponse.SUCCESS();
    }

    @Override
    public PageInfo<MemberAccountResult> searchByCondition(MemberAccountSearch memberAccountSearch) {
        PageHelper.startPage(memberAccountSearch.getOffset(), memberAccountSearch.getLimit());
        List<MemberAccountResult> memberAccountResults = memberAccountMapper.searchByCondition(memberAccountSearch);
        return new PageInfo<MemberAccountResult>(memberAccountResults);
    }
}
